<?php

defined('BY_JYA') or exit('error');
/*
	重复购
*/
$tabs=array(
	array('op'=>'list','title'=>'礼品列表'),
	array('op'=>'edit','title'=>'添加/编辑礼品'),
);
set_html('tabs',$tabs);
$_RQ['op']=$_RQ['op']?:'list';
$return['libao_types']=$this->libao_types;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_weishop_sale_libao',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if($item['type']==0){
				$item['title3']='优惠券';
				$item['title2']='赠送优惠券';
				$item['title5']='赠送优惠券';
			}elseif($item['type']==1){
				$item['title3']=$item['del_fee'];
				$item['title2']='立减'.$item['del_fee'];
				$item['title5']='立减'.$item['del_fee'];
			}elseif($item['type']==2){
				$item['title3']=$item['zhe_fee'];
				$item['title2']='打'.$item['del_fee'].'折';
				$item['title5']='打'.$item['del_fee'].'折';
			}elseif($item['type']==3){
				$item['title3']='送商品';
				$item['title2']='赠送商品';
				$item['title5']='赠送商品';
			}elseif($item['type']==4){
				$item['title3']='会员';
				$item['title2']='赠送会员';
				$item['title5']='赠送会员';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'tip'=>'[温馨提示]：礼包都在这里，点击使用，才会生效！！！','list'=>array(
			array('name'=>'title','title'=>'礼品标题','type'=>'string'),
			array('name'=>'logo','title'=>'礼品图片','type'=>'image'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'libao_types'),
			array('name'=>'price','title'=>'价值','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'领取时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'edit','title'=>'查看详情'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'礼品标题'),
			array('type'=>'select','width'=>15,'name'=>'type','title'=>'礼品类型','options'=>'libao_types'),
		),
	);
	$return['status']=array('未使用','已使用');
	$return['htmls']=$htmls;
	exi($return);
	
}
if($_RQ['op']=='edit'){
	$id = intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_sale_libao',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
		$item['type'] = '0';
	}
	$item['del_goods_ids']=$item['del_goods_ids']?json_decode($item['del_goods_ids'],true):array();
	$item['zhe_goods_ids']=$item['zhe_goods_ids']?json_decode($item['zhe_goods_ids'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'设置礼品详情',
		'look'=>array(
			array('name'=>'title','title'=>'礼品标题','type'=>'string'),
			array('name'=>'logo','title'=>'礼品LOGO','type'=>'image'),
			array('name'=>'price','title'=>'礼品价值','type'=>'string','unit'=>'元'),
			array('name'=>'type','title'=>'礼品类型','type'=>'radio','options'=>'libao_types'),
			array('type'=>'select','options'=>'levels','name'=>'member_levelid','title'=>'赠送会员等级','show'=>array('type'=>4)),
			array('name'=>'del_fee','title'=>'减免金额','type'=>'string','unit'=>'元','show'=>array('type'=>1),'help'=>''),
			array('name'=>'zhe_fee','title'=>'折扣权益%','type'=>'string','unit'=>'%','show'=>array('type'=>2),'help'=>'例如：填写8.5表示打85折'),
		),
	);
	$return['levels']=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_sale_libao',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}